package com.example.good.controller;

import com.example.good.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
//@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class GlobalErrorController implements ErrorController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;
    @Autowired
    HttpSession session;

    @RequestMapping("/error")
    public JsonResult<Void> errorhtml() throws IOException {
        System.out.println(request.getHeader("referer"));
//        response.sendRedirect("/");
        Throwable e = new Throwable();
        JsonResult<Void> result = new JsonResult<>(e);
//        session.setAttribute("uri",request.getRequestURI());
        result.setState(2000);
        result.setMessage((String) session.getAttribute("uri"));
        return result;
    };
}
